package com.lancoo.ccas53.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas53.entity.RuleUsable;
import com.lancoo.ccas53.mapper.RuleUsableMapper;
import com.lancoo.ccas53.service.RuleUsableService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 基础规则 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Service
public class RuleUsableServiceImpl extends ServiceImpl<RuleUsableMapper, RuleUsable> implements RuleUsableService {

    /**
     * @param taskId:
     * @Description: 获取基础规则
     * @Author: liufeng
     * @Date: 2024/6/13 10:26
     * @return:
     */
    @Override
    public List<RuleUsable> getBasicRule(Integer taskId) {
        List<RuleUsable> list = this.lambdaQuery().eq(RuleUsable::getTaskId, taskId).list();
        if (CollUtil.isEmpty(list)) {
            list = initBaseRule(taskId);
            this.saveOrUpdateBatch(list);
        }
        return list;
    }

    @Override
    public List<RuleUsable> initBaseRule(Integer taskId) {
        List<RuleUsable> list = new ArrayList<>();
        for (int i = 1; i <= 7; i++) {
            if (i == 6) {
                list.add(new RuleUsable().setTaskId(taskId)
                        .setCode(i)
                        .setOptionType(1111));
                continue;
            }
            list.add(new RuleUsable().setTaskId(taskId)
                    .setCode(i)
                    .setOptionType(0));
        }
        return list;
    }

    /**
     * @param taskId:
     * @param ruleUsableList:
     * @Description: 设置基础规则
     * @Author: liufeng
     * @Date: 2024/6/14 11:50
     * @return:
     */
    @Override
    public void setBasicRule(Integer taskId, List<RuleUsable> ruleUsableList) {
        this.saveOrUpdateBatch(ruleUsableList);
    }
}
